home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / GD / GD1.2 / GDDEMO.C < prev    next >
C/C++ Source or Header  |  1999-09-11  |  3KB  |  114 lines

  1. #include <stdio.h>
  2. #include "gd.h"
  3. #include "gdfontg.h"
  4. #include "gdfonts.h"
  5.  
  6. int main(void)
  7. {
  8.     /* Input and output files */
  9.     FILE *in;
  10.     FILE *out;
  11.  
  12.     /* Input and output images */
  13.     gdImagePtr im_in, im_out;
  14.  
  15.     /* Brush image */
  16.     gdImagePtr brush;
  17.  
  18.     /* Color indexes */
  19.     int white;
  20.     int blue;
  21.     int red;
  22.     int green;
  23.  
  24.     /* Points for polygon */
  25.     gdPoint points[3];
  26.  
  27.     /* Create output image, 128 by 128 pixels. */
  28.     im_out = gdImageCreate(128, 128);
  29.  
  30.     /* First color allocated is background. */
  31.     white = gdImageColorAllocate(im_out, 255, 255, 255);
  32.  
  33.     /* Set transparent color. */
  34.     gdImageColorTransparent(im_out, white);
  35.  
  36.     /* Try to load demoin.gif and paste part of it into the
  37.         output image. */
  38.  
  39.     in = fopen("demoin.gif", "rb");
  40.     if (!in) {
  41.         fprintf(stderr, "Can't load source image; this demo\n");
  42.         fprintf(stderr, "is much more impressive if demoin.gif\n");
  43.         fprintf(stderr, "is available.\n");
  44.         im_in = 0;
  45.     } else {
  46.         im_in = gdImageCreateFromGif(in);
  47.         fclose(in);
  48.         /* Now copy, and magnify as we do so */
  49.         gdImageCopyResized(im_out, im_in, 
  50.             16, 16, 0, 0, 96, 96, 127, 127);        
  51.     }
  52.     red = gdImageColorAllocate(im_out, 255, 0, 0);
  53.     green = gdImageColorAllocate(im_out, 0, 255, 0);
  54.     blue = gdImageColorAllocate(im_out, 0, 0, 255);
  55.     /* Rectangle */
  56.     gdImageLine(im_out, 8, 8, 120, 8, green);    
  57.     gdImageLine(im_out, 120, 8, 120, 120, green);    
  58.     gdImageLine(im_out, 120, 120, 8, 120, green);    
  59.     gdImageLine(im_out, 8, 120, 8, 8, green);    
  60.     /* Circle */
  61.     gdImageArc(im_out, 64, 64, 30, 10, 0, 360, blue);
  62.     /* Arc */
  63.     gdImageArc(im_out, 64, 64, 20, 20, 45, 135, blue);
  64.     /* Flood fill */
  65.     gdImageFill(im_out, 4, 4, blue);
  66.     /* Polygon */
  67.     points[0].x = 32;
  68.     points[0].y = 0;
  69.     points[1].x = 0;
  70.     points[1].y = 64;    
  71.     points[2].x = 64;
  72.     points[2].y = 64;    
  73.     gdImageFilledPolygon(im_out, points, 3, green);
  74.     /* Brush. A fairly wild example also involving a line style! */
  75.     if (im_in) {
  76.         int style[8];
  77.         brush = gdImageCreate(8, 8);
  78.         gdImageCopyResized(brush, im_in,
  79.             0, 0, 0, 0, 
  80.             gdImageSX(brush), gdImageSY(brush),
  81.             gdImageSX(im_in), gdImageSY(im_in));
  82.         gdImageSetBrush(im_out, brush);    
  83.         /* With a style, so they won't overprint each other.
  84.             Normally, they would, yielding a fat-brush effect. */
  85.         style[0] = 0;
  86.         style[1] = 0;
  87.         style[2] = 0;
  88.         style[3] = 0;
  89.         style[4] = 0;
  90.         style[5] = 0;
  91.         style[6] = 0;
  92.         style[7] = 1;
  93.         gdImageSetStyle(im_out, style, 8);
  94.         /* Draw the styled, brushed line */
  95.         gdImageLine(im_out, 0, 127, 127, 0, gdStyledBrushed);
  96.     }
  97.     /* Text */
  98.     gdImageString(im_out, gdFontGiant, 16, 16, "hi", red);
  99.     gdImageStringUp(im_out, gdFontSmall, 32, 32, "hi", red);
  100.     /* Make output image interlaced (allows "fade in" in some viewers,
  101.         and in the latest web browsers) */
  102.     gdImageInterlace(im_out, 1);
  103.     out = fopen("demoout.gif", "wb");
  104.     /* Write GIF */
  105.     gdImageGif(im_out, out);
  106.     fclose(out);
  107.     gdImageDestroy(im_out);
  108.     if (im_in) {
  109.         gdImageDestroy(im_in);
  110.     }
  111.     return 0;
  112. }
  113.  
  114.